package info.niwota.web.apache;

import info.niwota.web.Debug;
import info.niwota.web.proxy.ProxyHandler;
import x.org.apache.http.HttpRequest;
import x.org.apache.http.protocol.HttpRequestHandler;
import android.content.Context;
import android.util.Log;

/**
 * @author qiangli
 *
 */
public class HttpHandlerRegistry implements WebRequestHandlerResolver {
	private static final String TAG = "HttpHandlerRegistry";
	
	private final HttpAdapter adapter;

	private final ProxyHandler proxy;
	
	public HttpHandlerRegistry(Context ctx) {
		this.adapter = new HttpAdapter(ctx);
		this.proxy = new ProxyHandler(ctx);
	}

	public HttpRequestHandler lookup(HttpRequest request) {
		boolean b = HttpUtils.isProxyRequest(request);
		final HttpRequestHandler handler = b ? proxy : adapter;
		
		if (Debug.DEBUG) {
			Log.d(TAG, "lookup request: " + request.getRequestLine() + " proxy: " + b + " handler: " + handler);
		}
		
		return handler;
	}
	
	public HttpRequestHandler lookup(String uri) {
		throw new RuntimeException("use custom lookup(HttpRequest)");
	}
}
